package org.abc.fund.entity.user;

import jakarta.persistence.*;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import java.time.LocalDateTime;
import java.util.Set;

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Table(name="t_user")
public class User {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  @NotBlank
  @Size(min = 3, max = 50)
  @Column(unique = true, nullable = false)
  private String username;

  @NotBlank
  @Size(min = 8, max = 100) // 存储的是加密后的密码，所以长度要足够
  @Column(nullable = false)
  private String password;

  @NotBlank
  @Email
  @Column(nullable = false)
  private String email;

  @NotBlank
  @Column(nullable = false)
  private String realName;

  @ElementCollection(fetch = FetchType.EAGER)
  @CollectionTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"))
  @Enumerated(EnumType.STRING)
  @NotEmpty
  private Set<Role> roles;

  @Enumerated(EnumType.STRING)
  private Gender gender;

  private int age;

  @CreationTimestamp
  @Column(updatable = false)
  private LocalDateTime createdAt;

  @UpdateTimestamp private LocalDateTime updatedAt;

  public enum Gender {
    MALE,
    FEMALE
  }
}
